package com.example.animationmodule.livestartnumber
import android.animation.Animator
import android.animation.ValueAnimator
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.View
import android.view.animation.DecelerateInterpolator
import android.view.animation.Interpolator
import androidx.appcompat.widget.AppCompatTextView

/**
 * @author: Mr.You
 * @create: 2023-01-11 13:31
 * @description:
 **/
class LiveStartCountdownTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : AppCompatTextView(context, attrs, defStyleAttr),
    View.OnAttachStateChangeListener{

    private var playing: Boolean = false
    private var outAnimation: ValueAnimator? = null
    private var enterAnimation: ValueAnimator? = null

    var time = 4

    /**
     * 进度条通知。
     */
    private var mCountdownListener: OnCountdownListener? = null

    init {
        this.addOnAttachStateChangeListener(this)
        time = 4

        addTextChangedListener(object : TextWatcher{

            override fun afterTextChanged(p0: Editable?) {
                // 属性动画

                outAnimation = ValueAnimator.ofFloat(1.0f, 0.8f, 0.6f, 0.4f, 0f, 0f, 0f, 0f, 0f)
                outAnimation?.duration = 500L
                outAnimation?.interpolator = DecelerateInterpolator()
                outAnimation?.addUpdateListener {
                    val values = it.animatedValue
                    this@LiveStartCountdownTextView.scaleX = (values as Float)
                    this@LiveStartCountdownTextView.scaleY = (values as Float)
                }
                outAnimation?.addListener(object :Animator.AnimatorListener{
                    override fun onAnimationStart(animation: Animator) {

                    }

                    override fun onAnimationEnd(animation: Animator) {
                        outAnimation?.cancel()
                        if (time > 1 && playing) {
                            startAnimal()
                        } else {
                            if (mCountdownListener != null && playing) mCountdownListener?.onEnd()
                        }
                    }

                    override fun onAnimationCancel(animation: Animator) {

                    }

                    override fun onAnimationRepeat(animation: Animator) {

                    }


                })
                val enterFloatArray = floatArrayOf( 0f,0.4f, 0.6f, 0.8f, 1.0f, 1.0f, 1.0f, 1.0f)
                enterAnimation = ValueAnimator.ofFloat(*enterFloatArray)
                enterAnimation?.duration = 500L
                enterAnimation?.interpolator = DecelerateInterpolator()
                enterAnimation?.addUpdateListener {
                    val value = it.animatedValue
                    this@LiveStartCountdownTextView.scaleX = (value as Float)
                    this@LiveStartCountdownTextView.scaleY = (value as Float)
                }
                enterAnimation?.addListener(object :Animator.AnimatorListener{
                    override fun onAnimationStart(animation: Animator) {

                    }

                    override fun onAnimationEnd(animation: Animator) {
                        outAnimation?.start()
                    }

                    override fun onAnimationCancel(animation: Animator) {

                    }

                    override fun onAnimationRepeat(animation: Animator) {

                    }

                })
                enterAnimation?.start()
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

        })
    }


    override fun onVisibilityChanged(changedView: View, visibility: Int) {
        super.onVisibilityChanged(changedView, visibility)
    }

    override fun addOnAttachStateChangeListener(listener: OnAttachStateChangeListener?) {
        super.addOnAttachStateChangeListener(listener)
    }


    fun setCountdownListener(
        mCountdownListener: OnCountdownListener?
    ) {
        this.mCountdownListener = mCountdownListener
    }

    fun startAnimal() {
        time -= 1
        text = time.toString()
    }
    fun start() {
        playing = true
        time -= 1
        text = time.toString()
    }

    fun stopAnimal() {
        playing = false
        outAnimation?.cancel()
        enterAnimation?.cancel()
    }

    interface OnCountdownListener {
        /**
         * 进度通知。
         */
        fun onEnd()
    }

    override fun onViewAttachedToWindow(v: View) {

    }

    override fun onViewDetachedFromWindow(v: View) {

    }


}
